package com.niu1078.joe.ec.sign;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.niu1078.joe.delegates.JoeDelegate;
import com.niu1078.joe.ec.R;
import com.niu1078.joe.ec.R2;
import com.niu1078.joe.ec.database.DatabaseManager;
import com.niu1078.joe.ec.database.UserProfile;
import com.niu1078.joe.mylistener.MyScroller;
import com.niu1078.joe.mylistener.MyTextWatcher;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;

/**
 * author :ywq .
 * time: 2017/12/19:17:23.
 * desc :登录界面
 * action:
 */

public class SignInDelegate extends JoeDelegate {

    @BindView(R2.id.iv_logo)
    ImageView ivLogo;
    @BindView(R2.id.viewpager)
    ViewPager viewpager;
    @BindView(R2.id.username)
    AutoCompleteTextView username;
    @BindView(R2.id.password)
    AutoCompleteTextView password;
    @BindView(R2.id.forgot_password)
    TextView forgotPassword;
    @BindView(R2.id.ll_login)
    LinearLayout llLogin;
    @BindView(R2.id.sign_in_button)
    TextView signInButton;
    @BindView(R2.id.login_phone)
    ImageView loginPhone;
    @BindView(R2.id.login_weixin)
    ImageView loginWeixin;
    @BindView(R2.id.phone_login_form)
    LinearLayout phoneLoginForm;
    @BindView(R2.id.iv_black)
    ImageView ivBlack;
    @BindView(R2.id.tv_reg)
    TextView tvReg;
    private boolean isLogin;


    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_up;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        /**
         * 1.加载轮播图
         */
        initDatas();



        username.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                btnIsEnable();
            }
        });
        password.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                btnIsEnable();
            }
        });

      loginPhone.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startanimation();
          }
      });
signInButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        //存储数据库
        DatabaseManager.getInstance().getDao().deleteAll();
        DatabaseManager.getInstance().getDao().insert(new UserProfile(1,"测试","2314321432","ywq","fdsafdsafasdf"));
    }
});
    }


    private void startanimation2() {
        isLogin = false;
        ivBlack.setImageResource(R.mipmap.icon_close);//icon_close

        downLogin(forgotPassword);
        downLogin(signInButton);
        DownllLogin();

    }
    private void startanimation() {

        isLogin = true;
       // ivBlack.setImageResource(R.mipmap.icon_xiangyou);
        //1.隐藏viewpager  不能gone 需要站位
        upViewpager();
        //2.输入密码 手机的布局显示 上移

        //3.微信登录 和 手机号登录的布局 下移 并且消失
        downLoginweixin();
        downLoginPhone();


    }

    /**
     * 点击的时候调用
     */
    private void checkFrom() {
        String name = username.getText().toString().trim();
        String pwd = password.getText().toString().trim();

        if (name.isEmpty()) {
            username.setError("请输入用户手机号");
        } else {
            if (pwd.isEmpty()) {
                password.setError("请输入密码");
            }
        }
    }

    private void btnIsEnable() {

        if (username.getText().toString().trim().isEmpty() || password.getText().toString().trim().isEmpty()) {
            signInButton.setEnabled(false);
        } else {
            signInButton.setEnabled(true);
        }

    }
    private void initDatas() {
        //加载轮播图
        for (int i = 0; i < 10; i++) {
            ImageView imageView = (ImageView) View.inflate(getProxyActivity(), R.layout.image, null);
            ImageView imageView2 = (ImageView) View.inflate(getProxyActivity(), R.layout.image, null);
            ImageView imageView3 = (ImageView) View.inflate(getProxyActivity(), R.layout.image, null);

            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView2.setScaleType(ImageView.ScaleType.CENTER);
            imageView3.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setImageResource(R.drawable.g1_1);
            imageView2.setImageResource(R.drawable.g1_2);
            imageView3.setImageResource(R.drawable.g1_3);
            list.add(imageView);
            list.add(imageView2);
            list.add(imageView3);

        }

        // viewpager.setPageTransformer(true,new ZoomInTransformer());
        //  viewpager.setPageTransformer(false,new ZoomOutSlideTransformer());
        viewpager.setPageTransformer(false, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(View view, float position) {
                final float scale = 1f - Math.abs(position);
                final float scale1 = 1f + Math.abs(position);
                view.setScaleX(scale);
                view.setScaleY(scale);
                view.setPivotX(view.getWidth() * 0.5f);
                view.setPivotY(view.getHeight() * 0.5f);
                view.setAlpha(position < -1f || position > 1f ? 0f : 1f - (scale1 - 1f));
                if(position == -1){
                    //    view.setTranslationX(view.getWidth() * -1);
                }
            }
        });
        setViewPagerScrollSpeed(viewpager, 4000);

        viewpager.setAdapter(new MyAdapter());
        mHandler.sendEmptyMessageDelayed(AUTO, delay);
    }

    private void setViewPagerScrollSpeed(ViewPager viewPager, int speed) {
        try {
            Field field = ViewPager.class.getDeclaredField("mScroller");
            field.setAccessible(true);
            MyScroller viewPagerScroller = new MyScroller(viewPager.getContext(), new OvershootInterpolator(0.8F));
            field.set(viewPager, viewPagerScroller);
            viewPagerScroller.setDuration(speed);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private final long delay = 4 * 1000;

    private final int AUTO = 0;
    private  final Handler mHandler = new Handler() {

        @Override
        public void dispatchMessage(Message msg) {

            switch (msg.what) {
                case AUTO:

                    int index = viewpager.getCurrentItem();

                    viewpager.setCurrentItem((index + 1) % list.size());

                    mHandler.sendEmptyMessageDelayed(AUTO, 7000);

                    break;

                default:
                    break;
            }

        }


    };
    private void downLogin(final View myView) {


        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(myView, "alpha", 1.0f, 0.8f, 0.6f, 0.4f, 0.2f, 0.0f);
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(myView, "translationY", getResources().getDimension(R.dimen.dp_360), 0);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, transYAnim);
//                set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
        set.setDuration(500);
        set.start();

        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                if (myView.equals(signInButton)) {
                    signInButton.setVisibility(View.GONE);
                    downViewpager();
                    upLoginPhone(loginPhone);
                    upLoginweixin(loginWeixin);
                }
                if (myView.equals(forgotPassword)) {
                    forgotPassword.setVisibility(View.GONE);
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }

    //往上移动
    public void upLogin(final View myView) {
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(myView, "alpha", 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f);

        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(myView, "translationY", 0, -getResources().getDimension(R.dimen.dp_360));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, transYAnim);
        set.setDuration(600);
        set.start();

    }


    private void downLoginweixin() {


        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(loginWeixin, "alpha", 1.0f, 0.8f, 0.6f, 0.4f, 0.2f, 0.0f);
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(loginWeixin, "translationY", 0, getResources().getDimension(R.dimen.dp_160));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, transYAnim);
//                set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
        set.setDuration(400);
        set.start();
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {


            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

    }

    private void downLoginPhone() {


        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(loginPhone, "alpha", 1.0f, 0.8f, 0.6f, 0.4f, 0.2f, 0.0f);
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(loginPhone, "translationY", 0, getResources().getDimension(R.dimen.dp_160));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, transYAnim);
//                set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
        set.setDuration(500);
        set.start();
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {


                signInButton.setVisibility(View.VISIBLE);
                forgotPassword.setVisibility(View.VISIBLE);
                upllLogin();
                upLogin(signInButton);
                upLogin(forgotPassword);


            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

    }

    private void upLoginPhone(View view) {


        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(view, "alpha", 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f);
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(view, "translationY", getResources().getDimension(R.dimen.dp_160), 0);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, transYAnim);
//                set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
        set.setDuration(400);
        set.start();


    }

    private void upLoginweixin(View view) {


        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(view, "alpha", 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f);
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(view, "translationY", getResources().getDimension(R.dimen.dp_160), 0);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, transYAnim);
//                set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
        set.setDuration(500);
        set.start();


    }

    public void upViewpager() {
        viewpager.setVisibility(View.INVISIBLE);
//        viewpager
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(viewpager, "translationY", 0, -getResources().getDimension(R.dimen.dp_360));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(transYAnim);
        set.setDuration(500);
        set.start();
    }

    public void downViewpager() {

//        viewpager
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(viewpager, "translationY", -getResources().getDimension(R.dimen.dp_360), 0);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(transYAnim);
        set.setDuration(500);
        set.start();
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                viewpager.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }

    public void upllLogin() {
        llLogin.setVisibility(View.VISIBLE);
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(llLogin, "alpha", 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f);

        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(llLogin, "translationY", 0, -getResources().getDimension(R.dimen.dp_360));
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, transYAnim);
        set.setDuration(400);
        set.start();

    }

    public void DownllLogin() {
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(llLogin, "alpha", 1.0f, 0.8f, 0.6f, 0.4f, 0.2f, 0.0f);

        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(llLogin, "translationY", -getResources().getDimension(R.dimen.dp_360), 0);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, transYAnim);
        set.setDuration(400);
        set.start();
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                llLogin.setVisibility(View.GONE);

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }

    ArrayList<ImageView> list = new ArrayList();

    /**
     * viewpager的适配器
     */
    class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // 把对应位置的图片添加到ViewPager中
            ImageView imageView = list.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }

}
